package com.example.night.service.MysqlService;

import com.example.night.data.InfluencerCoins.WithdrawRecord;
import com.example.night.data.InfluencerCoins.WithdrawRecord.WithdrawStatus;
import com.example.night.data.InfluencerCoins.WithdrawRecordRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class WithdrawRecordMysqlService {

    private final WithdrawRecordRepository withdrawRecordRepository;

    @Autowired
    public WithdrawRecordMysqlService(WithdrawRecordRepository withdrawRecordRepository) {
        this.withdrawRecordRepository = withdrawRecordRepository;
    }

    // 添加提现记录
    public WithdrawRecord addWithdrawRecord(WithdrawRecord record) {
        try {
            WithdrawRecord savedRecord = withdrawRecordRepository.save(record);
            log.info("添加提现记录成功：{}", savedRecord);
            return savedRecord;
        } catch (Exception e) {
            log.error("添加提现记录失败", e);
            return null;
        }
    }

    // 根据 ID 查询提现记录
    public Optional<WithdrawRecord> getWithdrawRecordById(Long id) {
        try {
            return withdrawRecordRepository.findById(id);
        } catch (Exception e) {
            log.error("根据 ID 查询提现记录失败", e);
            return Optional.empty();
        }
    }

    // 根据抖音 ID 查询提现记录
    public List<WithdrawRecord> getWithdrawRecordsByPromoteId(String promoteId) {
        try {
            return withdrawRecordRepository.findByPromoteId(promoteId);
        } catch (Exception e) {
            log.error("根据抖音 ID 查询提现记录失败", e);
            return List.of();
        }
    }

    // 根据提现状态查询提现记录
    public List<WithdrawRecord> getWithdrawRecordsByStatus(WithdrawStatus status) {
        try {
            return withdrawRecordRepository.findByStatus(status);
        } catch (Exception e) {
            log.error("根据提现状态查询提现记录失败", e);
            return List.of();
        }
    }

    // 根据日期范围查询提现记录
    public List<WithdrawRecord> getWithdrawRecordsByDateRange(LocalDateTime startDate, LocalDateTime endDate) {
        try {
            return withdrawRecordRepository.findByApplyTimeBetween(startDate, endDate);
        } catch (Exception e) {
            log.error("根据日期范围查询提现记录失败", e);
            return List.of();
        }
    }


    // 更新提现状态
    public boolean updateWithdrawStatus(Long id, WithdrawStatus newStatus) {
        try {
            Optional<WithdrawRecord> recordOptional = withdrawRecordRepository.findById(id);
            if (recordOptional.isPresent()) {
                WithdrawRecord record = recordOptional.get();
                record.setWithdrawStatus(newStatus);
                withdrawRecordRepository.save(record);
                log.info("更新提现状态成功：ID={}，新状态={}", id, newStatus);
                return true;
            } else {
                log.warn("未找到 ID 为 {} 的提现记录", id);
                return false;
            }
        } catch (Exception e) {
            log.error("更新提现状态失败", e);
            return false;
        }
    }

    // 删除提现记录
    public boolean deleteWithdrawRecord(Long id) {
        try {
            if (withdrawRecordRepository.existsById(id)) {
                withdrawRecordRepository.deleteById(id);
                log.info("删除提现记录成功：ID={}", id);
                return true;
            } else {
                log.warn("提现记录不存在：ID={}", id);
                return false;
            }
        } catch (Exception e) {
            log.error("删除提现记录失败", e);
            return false;
        }
    }
    // 获取所有提现记录
    public List<WithdrawRecord> getAllWithdrawRecords() {
        try {
            return withdrawRecordRepository.findAll();
        } catch (Exception e) {
            log.error("获取所有提现记录失败", e);
            return List.of();
        }
    }

}
